﻿Public Class frmRecibosListado

    Private Sub frmVentasListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        fechaInicio.Value = DateTime.Now.AddDays(-30)
        fechaFin.Value = DateTime.Now
        dgVentas.AutoGenerateColumns = False
        CargarRecibos()

    End Sub

    Dim controller As New ControllerSigeco.controller_ventas()

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(ByVal value As Integer)
            _codCliente = value
        End Set
    End Property

    Private m_frmVentas As frmRecibos
    Public Property frmVentas() As frmRecibos
        Get
            Return m_frmVentas
        End Get
        Set(ByVal value As frmRecibos)
            m_frmVentas = value
        End Set
    End Property

    Private Sub CargarRecibos()

        Dim dt As New DataTable
        dt = controller.get_Recibos_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, _
                                    txtProveedor.Text, cboEstado.SelectedValue, _
                                    txtNroComprobante.Text, "", codCliente, True)
        dgVentas.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgVentas.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgVentas.SelectedRows(0).Cells(0).Value

                If frmVentas Is Nothing Then

                    frmVentas = New frmRecibos
                    frmVentas.codRecibo = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmVentas.ShowDialog()
                    CargarRecibos()
                    frmVentas.Dispose()
                    frmVentas = Nothing
                    'End If
                Else
                    frmVentas.codVenta = cod
                    frmVentas.ShowDialog()
                    CargarRecibos()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarRecibos()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNroComprobante.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarRecibos()

        End If
    End Sub

    Private Sub dgVentas_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgVentas.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgVentas_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgVentas.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmVentas = New frmRecibos()
        frmVentas.codRecibo = 0
        frmVentas.ShowDialog()
        CargarRecibos()
    End Sub

    Private m_frmBusCliente As frmClienteBuscador
    Public Property frmBusCliente() As frmClienteBuscador
        Get
            Return m_frmBusCliente
        End Get
        Set(ByVal value As frmClienteBuscador)
            m_frmBusCliente = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click
        frmBusCliente = New frmClienteBuscador
        frmBusCliente.ShowDialog()
        txtProveedor.Text = frmBusCliente.ClienteDesc
        codCliente = frmBusCliente.codCliente
        If codCliente > 0 Then
            CargarRecibos()
        End If
    End Sub

    Private Sub btnCancelProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelProveedor.Click
        codCliente = 0
        txtProveedor.Text = ""
        CargarRecibos()
    End Sub

    Private m_frmReporte As frmReportes
    Public Property frmRpts() As frmReportes
        Get
            Return m_frmReporte
        End Get
        Set(ByVal value As frmReportes)
            m_frmReporte = value
        End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "RECIBOS"
            frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "RECIBOS"
                frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "RECIBOS"
                frmRpts.datos_Reporte = CType(dgVentas.DataSource, DataTable).Copy
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If

    End Sub
End Class